package com.zy.openchat.core.model.file;

import android.os.Parcel;
import android.os.Parcelable;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class FileInfoLocal implements Parcelable {
    public String path;
    public boolean can_be_downloaded;
    public boolean can_be_deleted;
    public boolean is_downloading_active;
    public boolean is_downloading_completed;
    public long download_offset;
    public long downloaded_prefix_size;
    //已下载大小
    public long downloaded_size;

    public boolean isExist() {
        if (!StringUtils.isEmpty(path)) {
            try {
                File f = new File(path);
                return f.exists();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.path);
        dest.writeByte(this.can_be_downloaded ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_be_deleted ? (byte) 1 : (byte) 0);
        dest.writeByte(this.is_downloading_active ? (byte) 1 : (byte) 0);
        dest.writeByte(this.is_downloading_completed ? (byte) 1 : (byte) 0);
        dest.writeLong(this.download_offset);
        dest.writeLong(this.downloaded_prefix_size);
        dest.writeLong(this.downloaded_size);
    }

    public FileInfoLocal() {
    }

    protected FileInfoLocal(Parcel in) {
        this.path = in.readString();
        this.can_be_downloaded = in.readByte() != 0;
        this.can_be_deleted = in.readByte() != 0;
        this.is_downloading_active = in.readByte() != 0;
        this.is_downloading_completed = in.readByte() != 0;
        this.download_offset = in.readLong();
        this.downloaded_prefix_size = in.readLong();
        this.downloaded_size = in.readLong();
    }

    public static final Creator<FileInfoLocal> CREATOR = new Creator<FileInfoLocal>() {
        @Override
        public FileInfoLocal createFromParcel(Parcel source) {
            return new FileInfoLocal(source);
        }

        @Override
        public FileInfoLocal[] newArray(int size) {
            return new FileInfoLocal[size];
        }
    };
}
